package com.demo.common;

import java.net.URL;

import com.demo.common.controller.BlogController;
import com.demo.common.model.Blog;
import com.demo.handler.HtmlHandler;
import com.demo.interceptor.GlobalInterceptor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;

public class CommonConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("config.properties"); // 加载少量必要配置，随后可用getProperty(...)获取值
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setViewType(ViewType.JSP); // 设置视图类型为Jsp，否则默认为FreeMarker
		me.setBaseViewPath("/WEB-INF/view");

	}

	@Override
	public void configRoute(Routes me) {
		me.add("/blog", BlogController.class);

	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型

	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new GlobalInterceptor());

	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new HtmlHandler());

	}
	@Override
	public void afterJFinalStart() {
		System.out.println("jfinal 启动后自动调用");
		super.afterJFinalStart();
	}
	@Override
	public void beforeJFinalStop() {
		System.out.println("jfinal 停止前自动调用");
		super.beforeJFinalStop();
	}
	public static void main(String[] args) {
		// by zyk 解决包冲突的问题
//		 URL urlOfClass =
//		 CommonConfig.class.getClassLoader().getResource("org/slf4j/spi/LocationAwareLogger.class");
//		 System.out.println(urlOfClass);
		JFinal.start("WebRoot", 5005, "/", 5);
	}

}
